package cz.gelion.shylock.invoice.entity

import javax.persistence.Entity
import javax.persistence.Table
import java.io.Serializable
import scala.beans.BeanProperty
import javax.persistence.ManyToOne
import javax.persistence.JoinColumn
import javax.persistence.EmbeddedId
import javax.persistence.Column

@Entity
@Table(name="invoice_item", schema="invoice")
class Item(i:Item.PK) {

  def this() = this(null)
  
  def this(i:Invoice, no:Int) = this(new Item.PK(i,no))
  
	 
  @EmbeddedId
  var id:Item.PK = i 
  
  def getId():Item.PK = {if (id == null) id = new Item.PK; id}
  
  @BeanProperty
  @Column(name="quantity")
  var quantity:Float = _
  
  @BeanProperty
  @ManyToOne
  @JoinColumn(name="unit")
  var unit:QuantityUnit = _
  
  @BeanProperty
  @Column(name="price")
  var price:Float = _
  
  @BeanProperty
  @Column(name="vat")
  var vat:Float = _
  
  @transient
  var invoice:Invoice = id.invoice
  
  def setNo(i:Int) = getId.no = i
  def getNo():Int = getId.no
  
    
}

object Item {
  class PK(i:Invoice, n:Int) extends Serializable {
    
    def this() = this(null,0)
    
    @BeanProperty
    @ManyToOne
    @JoinColumn(name="invoice")
    var invoice:Invoice = i
    
    @BeanProperty
    @Column(name="id")
    var no:Int = n
    
    
  }
  
  
}